//
//  FDBContentViewController.m
//  FDBSDK
//
//  Created by curer on 1/3/16.
//  Copyright © 2016 lion. All rights reserved.
//

#import "FDBContentViewController.h"
#import "FDBContactViewController.h"
#import "FDBResource.h"
#import "FDBSDK.h"
#import "FDBConfigure.h"

@interface FDBContentViewController ()
@property (strong, nonatomic) IBOutlet UITextView *textView;

@end

@implementation FDBContentViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.title = NSLocalizedString(@"意见反馈", nil);
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                          target:self
                                                                                          action:@selector(cancelButtonClicked:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                                                           target:self
                                                                                           action:@selector(saveButtonClicked:)];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.textView becomeFirstResponder];
    [self.textView scrollRangeToVisible:NSMakeRange(0, 1)];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

#pragma mark - action

- (void)cancelButtonClicked:(UIButton *)btn {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)saveButtonClicked:(UIButton *)btn {
    NSString *content = self.textView.text;
    if (content.length == 0) {
        return;
    }
    
    if ([[FDBConfigure sharedInstance] hasContact]) {
        
        [FDBSDK sendFeedbackWithContent:content withEmotionLevel:0];
        [self dismissViewControllerAnimated:YES completion:nil];
        
        return;
    }
    
    FDBContactViewController *vc = [[FDBContactViewController alloc] initWithNibName:@"FDBContactViewController"
                                                                              bundle:MYBUNDLE];
    vc.contact = self.textView.text;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
